package StudyCode.Application;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.prefs.Preferences;

/**
 * @author lixiaoyao
 * @date 2018/11/30 0030、下午 1:40
 * @description Preferences(首选项API)示例代码
 */
public class PreferencesTest {

	public static void main(String[] args) {
		PreferencesFrame preferencesFrame = new PreferencesFrame();
		preferencesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		preferencesFrame.setVisible(true);
	}

}

class PreferencesFrame extends JFrame {

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private Preferences root = Preferences.userRoot();
	private Preferences node = root.node("/com/horstman/corejava");

	public PreferencesFrame() {
		// get position,size,title from preferences

		int left = node.getInt("left", 0);
		int top = node.getInt("top", 0);
		int width = node.getInt("width", DEFAULT_WIDTH);
		int height = node.getInt("height", DEFAULT_HEIGHT);
		setBounds(left, top, width, height);

		// if no title given,ask user

		String title = node.get("title", "");
		if (title.equals("")) {
			title = JOptionPane.showInputDialog("Please supply a frame title:");
		}
		if (title == null) {
			title = "";
		}
		setTitle(title);

		// set up file chooser that shows XML fils

		final JFileChooser fileChooser = new JFileChooser();
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml"));

		// set up menus

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		JMenu menu = new JMenu("File");
		menuBar.add(menu);

		JMenuItem exportItem = new JMenuItem("Export preferences");
		menu.add(exportItem);
		exportItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (fileChooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
					try {
						savePreferences();
						OutputStream outputStream = new FileOutputStream(fileChooser.getSelectedFile());
						node.exportSubtree(outputStream);
						outputStream.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		});

		JMenuItem importItem = new JMenuItem("Import preferences");
		menu.add(importItem);
		importItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (fileChooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
					try {
						InputStream inputStream = new FileInputStream(fileChooser.getSelectedFile());
						Preferences.importPreferences(inputStream);
						inputStream.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		});

		JMenuItem exitItem = new JMenuItem("Exit");
		menu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				savePreferences();
				System.exit(0);
			}
		});

	}

	public void savePreferences() {
		node.putInt("left", getX());
		node.putInt("top", getY());
		node.putInt("width", getWidth());
		node.putInt("height", getHeight());
		node.put("title", getTitle());
	}

}
